package com.sixth.controller;


import com.sixth.annotation.ParameterValidation;
import com.sixth.annotation.OperationLogAdvice;
import com.sixth.bean.bo.AdminOrderReplyBO;
import com.sixth.bean.bo.OrderDetailBO;
import com.sixth.bean.bo.OrderListBO;
import com.sixth.bean.bo.OrderRefundBO;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.OrderChannelCodeAndNameVO;
import com.sixth.bean.vo.OrderDetailVO;
import com.sixth.bean.vo.OrderListVO;
import com.sixth.constant.Constant;
import com.sixth.exception.CustomValidationException;
import com.sixth.service.OrderService;
import com.sixth.util.ValidationUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.ValidationException;
import javax.validation.constraints.Max;
import java.util.*;

/**
 * 商场管理里的订单管理
 *
 * @Author lixinhang
 * @Time 2022/9/7 16:47
 */
@ParameterValidation
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;


    //订单的条件查询（根据user_id,order_sn,add_time,order_status来查询）
    @RequiresPermissions(value = {"admin:order:list"},logical = Logical.OR)
    @RequestMapping("list")

    public BaseRespVo orderList(@Valid OrderListBO orderListBO,BindingResult bindingResult,BasePageInfo basePageInfo) throws CustomValidationException {
        //先对输入的orderListBO中的user_id做一下校验
        ValidationUtils.valid(bindingResult);

        orderListBO.setBasePageInfo(basePageInfo);

        OrderListVO orderListVO = orderService.orderList(orderListBO);

        return BaseRespVo.ok(orderListVO);
    }

    //查询订单的详情
    @RequiresPermissions(value = {"admin:order:detail"},logical = Logical.OR)
    @RequestMapping("detail")
    public BaseRespVo orderDetail(@Valid OrderDetailBO orderDetailBO,BindingResult bindingResult) throws CustomValidationException {
        //先对输入的order_id做一下参数校验
        ValidationUtils.valid(bindingResult);

        //@Max(10000000)
        Integer id = orderDetailBO.getId();

        OrderDetailVO orderDetailVO = orderService.orderDetail(id);
        return  BaseRespVo.ok(orderDetailVO);
    }


    //获取<快递代号,快递名称>键值对，如<"ZTO","中通快递">
    @OperationLogAdvice(operationType = 2,operation = "编辑订单")
    @RequestMapping("channel")
    public BaseRespVo orderChannel(){
        List<OrderChannelCodeAndNameVO> orderChannelCodeAndNameVOS = new ArrayList<>();
        Set<String> keySet = Constant.CODE_NAME_MAP.keySet();
        for (String s : keySet) {
            orderChannelCodeAndNameVOS.add(new OrderChannelCodeAndNameVO(s, Constant.CODE_NAME_MAP.get(s)));
        }
        return BaseRespVo.ok(orderChannelCodeAndNameVOS);
    }

    //后台删除订单
    @OperationLogAdvice(operationType = 2,operation = "删除订单")
    @RequiresPermissions(value = {"admin:order:delete"},logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo orderDelete(@RequestBody Map map){
        Integer orderId = (int)(map.get("orderId")); //我也不想写魔法值
        orderService.orderDeleteById(orderId);
        return BaseRespVo.ok();
    }

    //发货
    @OperationLogAdvice(operationType = 2,operation = "发货")
    @RequiresPermissions(value = {"admin:order:ship"},logical = Logical.OR)
    @RequestMapping("ship")
    public BaseRespVo orderShip(@RequestBody Map map){
        orderService.orderShip(map);
        return BaseRespVo.ok();
    }

    //后台商家同意退款,同意退款要去修改库存
    @OperationLogAdvice(operationType = 2,operation = "商家同意退款")
    @RequiresPermissions(value = {"admin:order:refund"},logical = Logical.OR)
    @RequestMapping("refund")
    public BaseRespVo orderRefund(@RequestBody OrderRefundBO orderRefundBO){
        orderService.orderRefund(orderRefundBO);
        return BaseRespVo.ok();
    }

    /**
     *
     * @author qys
     * @date 2022/9/9 20:38
     * @param adminOrderReplyBO
     * @return BaseRespVo
     */
    @OperationLogAdvice(operationType = 2,operation = "回复")
    @PostMapping("reply")
    public BaseRespVo adminOrderReply(@RequestBody AdminOrderReplyBO adminOrderReplyBO) {
        Integer commentId = adminOrderReplyBO.getCommentId();
        String content = adminOrderReplyBO.getContent();

        if (content == null || "".equals(content)) {
            return BaseRespVo.invalidNoAndMsg("参数不对", 401);
        }
        int code = orderService.adminOrderReply(commentId, content);
        if (code == 622) {
            return BaseRespVo.invalidNoAndMsg("订单商品已回复！", 622);
        }
        return BaseRespVo.ok();
    }

}
